﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Configuration;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using BLL;
using Model;
using gfLibrary;
using System.Data;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Web.Services;
using DBUtility;

namespace IYAKASAPortal.Login
{
    /// <summary>
    /// Summary description for AjaxClass
    /// </summary>
    public class AjaxClass : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            StringBuilder _strContent = new StringBuilder();
            if (_strContent.Length == 0)
            {
                string _strAction = context.Request.Params["action"];
                if (string.IsNullOrEmpty(_strAction))
                {
                    _strContent.Append("{\"msg\": \"0\", \"msgbox\": \"禁止访问！\",\"rows\": []}");
                }
                else
                {
                    switch (_strAction.Trim().ToLower())
                    {
                        case "getinfo": 
                            _strContent.Append(GetInfo(context)); 
                            break;
                        case "getpp":
                            _strContent.Append(GetPP(context));
                            break;
                        case "getxh":
                            _strContent.Append(GetXH(context));
                            break;
                        case "getfdjxh":
                              _strContent.Append(GetFDJXH(context));
                              break;
                        case "getfdjxhex":
                              _strContent.Append(GetFDJXHEx(context));
                              break;
                        case "getxzqy":
                              _strContent.Append(GetXZQY(context));
                              break;
                        default: break;
                    }
                }
            }
            context.Response.Write(_strContent.ToString());
        }

        private string GetInfo(HttpContext context)
        {
            string strHPHM = context.Request["HPHM"].ToString();
            string strHPZL = context.Request["HPZL"].ToString();
            string strSql = "";
            if (strHPZL!="0")
            {
                strHPZL = context.Request["HPZL"].ToString();
                strSql = "select top 10 ID as id, substring(HPHM,3,5) as text from LOGIN_VEHICLE_INFO where  HPHM like '%" + strHPHM + "%'  and HPZLDH='" + strHPZL + "' order by ID desc";
            }
            else
            {
                strSql = "select top 10 ID as id, substring(HPHM,3,5) as text from CHARGE_INFO where  HPHM like '%" + strHPHM + "%' and SFLSH IS NOT NULL order by ID desc";
            }

            DataTable dt = new DataTable();
            DbHelper.GetTable(strSql,ref dt);

            string _json = JsonConvert.SerializeObject(dt);

            return _json;
        }

        private string GetPP(HttpContext context)
        {
            string strPP = context.Request["PP"].ToString();
            string strXH = context.Request["XH"].ToString();

            string strSql = "select distinct top 10 ID as id, PP as text from TYPE_CX_CONTENT where 1=1 ";

            if (!string.IsNullOrEmpty(strPP))
            {
                strSql += " and PP like '%" + strPP + "%' ";
            }

            if (!string.IsNullOrEmpty(strXH))
            {
                strSql += " and XH like '%" + strXH + "%' ";
            }

            DataTable dt = new DataTable();
            DbHelper.GetTable(strSql, ref dt);

            string _json = JsonConvert.SerializeObject(dt);

            return _json;

        }

        private string GetXH(HttpContext context)
        {
            string strPP = context.Request["PP"].ToString();
            string strXH = context.Request["XH"].ToString();

            string strSql = "select top 10 ID as id, XH as text from TYPE_CX_CONTENT where 1=1 ";

            if (!string.IsNullOrEmpty(strPP))
            {
                strSql += " and PP like '%" + strPP + "%' ";
            }

            if (!string.IsNullOrEmpty(strXH))
            {
                strSql += " and XH like '%" + strXH + "%' ";
            }

            DataTable dt = new DataTable();
            DbHelper.GetTable(strSql, ref dt);

            string _json = JsonConvert.SerializeObject(dt);

            return _json;
        }

        private string GetFDJXH(HttpContext context)
        {
            string strFDJXH = context.Request["FDJXH"].ToString();
            string strSql = "select distinct top 10 ID as id, FDJXH as text from TYPE_CX_FDJ_UNION where 1=1 ";

            if (!string.IsNullOrEmpty(strFDJXH))
            {
                strSql += " and FDJXH like '%" + strFDJXH + "%' ";
            }

            //if (!string.IsNullOrEmpty(strXH))
            //{
            //    strSql += " and XH like '%" + strXH + "%' ";
            //}

            DataTable dt = new DataTable();
            DbHelper.GetTable(strSql, ref dt);

            string _json = JsonConvert.SerializeObject(dt);

            return _json;
        }

        private string GetFDJXHEx(HttpContext context)
        {
            string strPP = context.Request["PP"].ToString();
            string strXH = context.Request["XH"].ToString();

            string strSql = "select distinct top 10 ID as id, FDJXH as text from TYPE_CX_FDJ_UNION where 1=1 ";

            if (!string.IsNullOrEmpty(strPP))
            {
                strSql += " and PP like '%" + strPP + "%' ";
            }

            if (!string.IsNullOrEmpty(strXH))
            {
                strSql += " and XH like '%" + strXH + "%' ";
            }

            DataTable dt = new DataTable();
            DbHelper.GetTable(strSql, ref dt);

            string _json = JsonConvert.SerializeObject(dt);

            return _json;
        }

        public string GetXZQY(HttpContext context)
        {
            string strXZQY = context.Request["XZQY"].ToString();
            string strSql = "SELECT top 20 XZQY as text, COUNT(XZQY) as id FROM [LOGIN_VEHICLE_INFO] where XZQY is not null and  XZQY<>'' ";
            if (string.IsNullOrEmpty(strXZQY) == false)
            {
                strSql += " and XZQY like '%" + strXZQY + "%' ";
            }

            strSql+=" group by XZQY order by COUNT(XZQY) desc";

            DataTable dt = new DataTable();
            DbHelper.GetTable(strSql, ref dt);

            string _json = JsonConvert.SerializeObject(dt);

            return _json;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}